package tgc.rj.zz.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.rj.zz.custom.AjaxResult;
import tgc.rj.zz.custom.DataGridUtils;
import tgc.rj.zz.entity.Journal;
import tgc.rj.zz.entity.LesseeAdmin;
import tgc.rj.zz.entity.SysRole;
import tgc.rj.zz.entity.SysUser;
import tgc.rj.zz.entity.User;
import tgc.rj.zz.form.UserForm;
import tgc.rj.zz.repository.RoleRepository;
import tgc.rj.zz.security.UserUtils;
import tgc.rj.zz.service.JournalService;
import tgc.rj.zz.service.LesseeAdminService;
import tgc.rj.zz.service.SysUserService;
import tgc.rj.zz.service.UserService;

@Controller
@RequestMapping(value="user")
public class UserController {
	@Autowired
	private UserService userDAO;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private LesseeAdminService lesseeAdminService;
	@Autowired
	private RoleRepository roleDAO;
	@Autowired
	private SysUserService sysUserDAO;
	@Autowired
	private JournalService service;
	
	@RequestMapping(value="/all")
	public String all() {
		return "admin/yh_all";
	}
	
	@RequestMapping(value="/cz")
	@ResponseBody
	public String cz(Integer id) {
		SysUser user = sysUserDAO.findById(id);
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		user.setPassword(encoder.encode("111111"));
		sysUserDAO.save(user);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String date = df.format(new Date());
		Journal journal = new Journal();
		journal.setDate(date);
		journal.setUsername(user.getUsername());
		journal.setOperationName("重置密码");
		service.save(journal);
		return "重置成功！";
	}
	
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(UserForm form) {
		Pageable pageable = form.buildPageable();
		Page<User> page = null;
		if(form.getId() == null) {
			page = userDAO.findAll(pageable);			
		}else {
			page = userDAO.findAll(form.buildSpes(), pageable);
		}
		HashMap<String , Object> result = DataGridUtils.getPageResult(page);
		return result;
	}
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(User model) {
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder(); 
		SysRole role=roleDAO.findById(3).get();
		SysUser user = userUtils.getUser();
		if((model.getPassword() == null || model.getPassword() == "")) {
			LesseeAdmin lesseeAdmin = lesseeAdminService.findById(user.getId());
			model.setPassword(encoder.encode("111111"));
			model.setSf(1);
			model.setLesseeAdmin(lesseeAdmin);			
		}
		model.getRoles().add(role);
		userDAO.save(model);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String date = df.format(new Date());
		Journal journal = new Journal();
		journal.setDate(date);
		journal.setUsername(user.getUsername());
		journal.setOperationName("编辑了用户");
		service.save(journal);
		return AjaxResult.build(true, "OK");
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public void delete(String ids) {
		SysUser user = userUtils.getUser();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String date = df.format(new Date());
		Journal journal = new Journal();
		journal.setDate(date);
		journal.setUsername(user.getUsername());
		journal.setOperationName("删除了用户");
		service.save(journal);
		String[] id = ids.split(",");
		for(int i = 0; i < id.length; i++) {
			userDAO.deleteById(Integer.parseInt(id[i]));
		}
	}
}
